import strategies from "./strategies";
import getPageName from "../getPageName";
window.layerY = "";
import store from "../../store";
export default {
    install(Vue, options = {}) {
        let _self = this;
        // console.log(Vue);
        Vue.directive("scter", {
            bind(el, binding, vnode) {
                //初始化拖拽放置事件
                // console.log(binding);
                // console.log("------", Vue);
                let _vue = vnode.context;
                // console.log("---123", vnode.context);
                if (_self.checkoutEnvironment()) {
                    // console.log("初始化拖拽区域的事件");
                    _self.initDragContainersEvent(_vue, el);
                }
            },
            componentUpdated(el, binding) {
                // el.innerHTML = binding.value.index;
                // console.log("container更新完毕");
            }
        });
    },
    //初始化拖拽放置事件
    initDragContainersEvent(Vue, dom) {
        // console.log("--------111", Vue);
        if (!dom) {
            console.log("初始化拖拽放置区域失败：没有拿到dom");
            return;
        }

        dom.ondragenter = (event) => {
            event.preventDefault();
        }

        dom.ondragover = (event) => {
            event.preventDefault();
        }

        dom.ondrop = (event) => {
            event.preventDefault();
            // console.log("drop事件", event);
            // console.log(window.dragName);
            // console.log("--------2222", Vue);
            window.layerY = event.pageY;
            this.appendComponent(Vue, dom);
        }

        // console.log("初始化拖拽放置区域成功");
    },
    //将拖拽的元素添加到放置区域
    appendComponent(Vue, dom) {
        let page = this.getPageName();
        // console.log()
         console.log(window.dragType);
        if (strategies[window.dragType]) {
            console.log(window.dragCom);
            // return;
           
            strategies[window.dragType](Vue, page, window.dragCom);
        }
            
    },
    //获取当前页面的路径名称
    getPageName() {
        let url = window.location.href;
        let arr = url.split("/");
        // console.log(arr);
        if (arr[arr.length - 1].indexOf("?") > -1) {
            return arr[arr.length - 1].indexOf("?").split("?")[0];
        }
        return arr[arr.length - 1];
    },
    checkoutEnvironment() {
        // console.log("0000000000000000000000000", window.environment);
        if (window.environment === "production") {
            return false;
        } else if (window.environment === "edit") {
            return true;
        }
    }
}